
//
//  MYRegisterFindPasswordCodeCell.swift
//  MuYing
//
//  Created by tbb on 2019/7/22.
//  Copyright © 2019 TBB. All rights reserved.
//

import UIKit

class MYRegisterFindPasswordCodeCell: MYRegisterFindPasswordTextFieldCell {
    @objc lazy var codeBtn: UIButton = {
        let codeBtn = UIButton(type: .custom);
        codeBtn.setTitleColor(UIColorFromHex(rgbValue: 0xCBCBCB), for: .normal)
        codeBtn.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: .medium)
        codeBtn.setTitle(kYQLSSwift("发送验证码"), for: .normal)
        codeBtn.frame = CGRect(x: 0, y: 0, width: 62, height: 21)

        return codeBtn;
    }()
    
    override func layoutSubviews() {
        super.layoutSubviews();
        self.textField.rightView = codeBtn;
        self.textField.rightViewMode = .always;
        self.textField.keyboardType = UIKeyboardType.phonePad;
        self.textField.autocapitalizationType = UITextAutocapitalizationType.none;
        self.textField.isSecureTextEntry = true;

        self.textField.rightViewMode = .always;
        //监听输入框的文本长度
        self.textField.addTarget(self, action:Selector(("textFieldDidChange:")), for: UIControl.Event.editingChanged)
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        
    }
    @objc func textFieldDidChange(_ textField: UITextField) {
        guard let _ = textField.text else { return }
        
        debugPrint("\(textField.text!)")
        let lang = textField.textInputMode?.primaryLanguage
        let tmpText = textField.text!
        if lang == "zh-Hans" {
            let range = textField.markedTextRange
            if range == nil {
                guard tmpText.count >= 6 else { return }
                textField.text = tmpText.mySubString(to: 6)
            }
        } else {
            guard tmpText.count >= 6 else { return }
            textField.text = tmpText.mySubString(to: 6)
        }
    }

}
